<?php 
session_start(); 
$websitePermission = 5;
include "headerVerify.php";
include "connect.php";
?>

<html>
<head>
<title>MyAccount | <?php echo $_SESSION['websiteAddress'] ?></title>

<link href="css/adminMain.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<link href="css/products.css" rel="stylesheet" type="text/css">

<script type="text/javascript">
function MM_changeProp(objId,x,theProp,theValue) { //v9.0
  var obj = null; with (document){ if (getElementById)
  obj = getElementById(objId); }
  if (obj){
    if (theValue == true || theValue == false)
      eval("obj.style."+theProp+"="+theValue);
    else eval("obj.style."+theProp+"='"+theValue+"'");
  }
}
</script>

<link href="../css/paginate.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="adminPageContainer">

<?php 

include "header.php";
include "navigation.php";

//Update message details - display if something has been updated
if ($_SESSION['updateMessage'] != "")
{
echo "<SCRIPT>";
echo "alert('".$_SESSION['updateMessage']."')";
echo "</SCRIPT>";
}

$_SESSION['updateMessage'] = "";

?>


<?php

	$tableName="products";		
	$targetpage = "products.php"; 	
	$limit = 4; 
	
	$query = "SELECT COUNT(*) as num FROM $tableName";
	$total_pages = mysql_fetch_array(mysql_query($query));
	$total_pages = $total_pages[num];
	
	$stages = 3;
	$page = mysql_escape_string($_GET['page']);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}	
	
    // Get page data
	$query1 = "SELECT * FROM $tableName LIMIT $start, $limit";
	$result = mysql_query($query1);
	
	// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;					
	
	
	$paginate = '';
	if($lastpage > 1)
	{	
	

	
	
		$paginate .= "<div class='paginate'>";
		// Previous
		if ($page > 1){
			$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
		}else{
			$paginate.= "<span class='disabled'>previous</span>";	}
			

		
		// Pages	
		if ($lastpage < 7 + ($stages * 2))	// Not enough pages to breaking it up
		{	
			for ($counter = 1; $counter <= $lastpage; $counter++)
			{
				if ($counter == $page){
					$paginate.= "<span class='current'>$counter</span>";
				}else{
					$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
			}
		}
		elseif($lastpage > 5 + ($stages * 2))	// Enough pages to hide a few?
		{
			// Beginning only hide later pages
			if($page < 1 + ($stages * 2))		
			{
				for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
			}
			// Middle hide some front and some back
			elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
			{
				$paginate.= "<a href='$targetpage?page=1'>1</a>";
				$paginate.= "<a href='$targetpage?page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
				$paginate.= "...";
				$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
				$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";		
			}
			// End only hide early pages
			else
			{
				$paginate.= "<a href='$targetpage?page=1'>1</a>";
				$paginate.= "<a href='$targetpage?page=2'>2</a>";
				$paginate.= "...";
				for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
				{
					if ($counter == $page){
						$paginate.= "<span class='current'>$counter</span>";
					}else{
						$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}					
				}
			}
		}
					
				// Next
		if ($page < $counter - 1){ 
			$paginate.= "<a href='$targetpage?page=$next'>next</a>";
		}else{
			$paginate.= "<span class='disabled'>next</span>";
			}
			
		$paginate.= "</div>";		
	
	
}

 // pagination
 echo $paginate;
?>







<?php 

while($row = mysql_fetch_array($result))
  {
  $productID = $row['productID'];
  $productCategory = $row['productCategory'];
  $productName = $row['productName'];
  $productDescription = $row['productDescription'];
  $productCost = $row['productCost'];
  $onTrial = $row['onTrial'];
  $customerName = $row['customerName'];
  $keywords = $row['keywords'];

  //creates the details on screen
  
  echo '<div id="adminContent">';
  echo '<form action="updateProductDetails.php" method="post" name="productDetails">';
  
  	   //Gets the URL of the page the user is currently on (For redirecting user back to the correct product page)
  echo '<input name="actual_link" type="hidden" value="http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI].'">';
  
  echo '<p>Product Category:</p>';
  echo '<input id="productIDVisable" name="productID" type="text" value="ID# '.$productID.'" readonly>';
  
  //Product ID thats passed through the form
  echo '<input name="productID" type="hidden" value="'.$productID.'">'; 
  
  //Lists all the product categories and runs IF statement to select the current category the product is assigned to
  echo '<select name="productCategory">';
  
  		$categoryOptions = mysql_query("SELECT * FROM categories ");

		while($option = mysql_fetch_array($categoryOptions))
 		{
		if ($option['categoryName'] == $productCategory)
			
			//If the option is the current products category then select it
			{echo '<option selected>'.$option['categoryName'].'</option>';} else			
			{echo '<option>'.$option['categoryName'].'</option>';}

		}
		
  echo '</select>';
  echo '<p>Product Name:</p>';
  echo '<input size="36" maxlength="36" name="productName" type="text" value="'.$productName.'">';
  echo '<p>Product Cost: £</p>';		
  echo '<input name="productCost" type="text" value="'.$productCost.'">';
  echo '<br /><br />';
  //Lists YES and NO and runs if statement to determine if it is on trial
  echo '<p>Saddle On Trial:</p>';
  echo '<select name="onTrial">';
  
  		if ($onTrial=="Yes")
			{echo '<option selected>Yes</option>
				   <option>No</option>';} else
		   
		    {echo '<option>Yes</option>
				   <option selected>No</option>';}
  		
  echo	'</select>';	
  echo '<input class="button" name="updateDetails" type="submit" value="Update Product Details">';	

		  //Lists all the customers and runs IF statement to select the current customer the product is assigned to


//Details hidden to assigne customer to the trial
  //echo '<select name="customerName">';
  
  		//$customers = mysql_query("SELECT * FROM customers ");

		//while($customer = mysql_fetch_array($customers))
 		//{
		//if ($customer['customerName'] == $customerName)
			
			//If the option is the current products category then select it
			//{echo '<option selected>'.$customer['customerName'].'</option>';} else			
			//{echo '<option>'.$customer['customerName'].'</option>';}

		//}
    		
  echo '<br /><br />';
  echo '<textarea name="productDescription">'.$productDescription.'</textarea>';
  echo '<p>Product Keywords:</p>';
  echo '<input id="keywordsField" name="keywords" type="text" value="'.$keywords.'">';
  
  //End product details form		
  echo '</form>';
  
  echo '<div id="imageControl">';
  echo '<br />';
  echo '<img src="http://www.24-7saddles.com/images/saddles/'.$productID.'_1.jpg" width="500" height="438" id="'.$productID.'_1">';
  echo '<img src="http://www.24-7saddles.com/images/saddles/'.$productID.'_2.jpg" width="500" height="438" id="'.$productID.'_2">';
  echo '<img src="http://www.24-7saddles.com/images/saddles/'.$productID.'_3.jpg" width="500" height="438" id="'.$productID.'_3">';
  echo '<img src="http://www.24-7saddles.com/images/saddles/'.$productID.'_4.jpg" width="500" height="438" id="'.$productID.'_4">';
  
    //Upload Image Form
  echo '<form action="uploadFile.php" method="post" name="uploadFile" enctype="multipart/form-data">';
  echo '<p>Upload Image:</p>';
  echo '<input id="uploadField" type="file" name="uploaded_file"> ';
  echo '<input name="productID" type="hidden" value="'.$productID.'">';
  echo '<p>Replace Image No: </p>';
  echo '<select name="imageNumber">
  		<option value="1" >1</option>
		<option value="2" >2</option>
		<option value="3" >3</option>
		<option value="4" >4</option>
		</select>';
  echo '<input id="button" class="button" name="uploadProductImage" type="submit" value="Upload Image">';
  	   //Gets the URL of the page the user is currently on (For redirecting user back to the correct product page)
  echo '<input name="actual_link" type="hidden" value="http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI].'">';
  echo '</form>';
  
  echo '</div>';
  
  //Update Product Details Form
  
  echo '<div id="productButtons">';	
   
  //Delete Product Form
  echo '<form action="deleteProduct.php" method="post" name="deleteProduct">';
  echo '<input name="productID" type="hidden" value="'.$productID.'">';
  echo '<input class="button" name="deleteProduct" type="submit" value="Delete This Product">';
  echo '</form>';	
  
  //End Product Buttons
  echo '</div>';
  
 
  //End Admin Content
  echo "</div>";
  

  
  }
?>
 
<br />

</div>
</body>
</html>